package com.ruoyi.web.controller.news;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.news.NewsScienceResult;
import com.ruoyi.system.domain.news.dto.NewsScienceResultDto;
import com.ruoyi.system.domain.news.query.NewsScienceResultQuery;
import com.ruoyi.system.domain.news.vo.NewsScienceResultVo;
import com.ruoyi.system.service.news.NewsScienceResultService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/news/newsScienceResult")
@Api(value = "newsScienceResultController", tags  = "科研成果")
public class NewsScienceResultController extends BaseController {

	@Resource
	private NewsScienceResultService newsScienceResultService;

    @ApiOperation(value = "新增/修改科研成果信息", notes = "新增/修改科研成果信息")
	@PostMapping("/submit")
	public R submit(@RequestBody NewsScienceResultDto newsScienceResultDto) {
		LoginUser loginUser = SecurityUtils.getLoginUser();
		NewsScienceResult newsScienceResult = new NewsScienceResult();
		BeanUtils.copyProperties(newsScienceResultDto,newsScienceResult);
		if (newsScienceResult.getId()==null) {
			newsScienceResult.setCreateBy(loginUser.getUsername());
			newsScienceResult.setDeptId(loginUser.getDeptId());
			newsScienceResult.setCreateTime(new Date());
			newsScienceResult.setUpdateBy(loginUser.getUsername());
			newsScienceResult.setUpdateTime(new Date());
		} else {
			newsScienceResult.setUpdateBy(loginUser.getUsername());
			newsScienceResult.setUpdateTime(new Date());
		}
		boolean result = newsScienceResultService.saveOrUpdate(newsScienceResult);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "删除科研成果信息", notes = "删除科研成果信息")
	@DeleteMapping("/delete")
	public R delete(@RequestParam(value = "id") Long id) {
		boolean result = newsScienceResultService.removeById(id);
		if (result) {
			return R.ok();
		} else {
			return R.fail("操作失败");
		}
	}

	@ApiOperation(value = "获取单个科研成果信息", notes = "获取单个科研成果信息")
	@GetMapping("/getById")
	public R<NewsScienceResultVo> getById(@RequestParam(value = "id") Long id) {
		NewsScienceResultQuery newsScienceResultQuery = new NewsScienceResultQuery();
		newsScienceResultQuery.setId(id);
		List<NewsScienceResultVo> newsScienceResultList = newsScienceResultService.getNewsScienceResultList(newsScienceResultQuery);
		if (newsScienceResultList!=null&&newsScienceResultList.size()>0) {
			return R.ok(newsScienceResultList.get(0));
		} else {
			return R.fail("数据不存在");
		}
	}

	@ApiOperation(value = "获取科研成果信息列表")
	@PostMapping(value = "/list")
	public R<PageInfo<NewsScienceResultVo>> list(@RequestBody NewsScienceResultQuery newsScienceResultQuery) {
		List<NewsScienceResultVo> newsScienceResultList = newsScienceResultService.getNewsScienceResultList(newsScienceResultQuery);
		return R.ok(new PageInfo<>(newsScienceResultList));
	}
}
